var var_a, var_b, var_c;
Page({
  data: {
    showFloorSwitch: true,
    showImageSwitch: true,
    formatIndex: 0,
    floorIndex: 0,
    formatPicker: ['0 - 25cm', '25 - 50 cm', '大于 50 cm'],
    floorPicker: [' 1-2 层', ' 2 层以上'],
    imgList: [],
    note: '',
    price: '0.00',
    disabled: false,
  },

  onLoad() {
    this.setData({ user: wx.appData.user });
    wx.get("getPrice", res => {
      this.setData({ priceMsg: res.msg })
      var_a = res.data.a,
      var_b = res.data.b,
      var_c = res.data.c;
      this.calcPrice();
    }, '正在读取信息')
    if (this.data.user.image) this.setData({ showImageSwitch: false });
  },

  submit(e) {
    let f = e.detail.value;
    f.unionid = this.data.user.unionid;
    f.price = this.data.price;
    f.format = this.data.formatIndex;
    f.floor = this.data.floorIndex;
    f.order_number = new Date().getTime() + wx.random(9);
    if (!(f.kd_name && f.kd_phone && f.building && f.format != null && f.note))
      return wx.toast('您的必要信息未填写完整');
    if (this.data.showFloorSwitch && f.floor == null && !f.value)
      return wx.toast('您的楼层信息未填写完整');
    if (!this.data.user.image && !this.data.imgList.length) 
      return wx.toast('您还未上传证件照');
    if (this.data.imgList.length) {
      wx.uploadFile({
        url: wx.Config.host + 'uploadImage',
        filePath: this.data.imgList[0],
        name: 'image',
        formData: {
          'unionid': f.unionid,
        },
      })
    }
    this.setData({ disabled: true })
    wx.call("payment", {
      command: "pay",
      out_trade_no: f.order_number,
      body: '快递代收',
      total_fee: f.price * 100
    }, payData => {
      wx.requestPayment({ 
        timeStamp: payData.result.timeStamp,
        nonceStr: payData.result.nonceStr,
        package: payData.result.package,
        signType: 'MD5',
        paySign: payData.result.paySign,
        success: () => {
          wx.post("addOrder", f, res => {
            this.setData({ disabled: false })
            if (res.ok) {
              wx.toast('订单已提交', 1, 1, () => {
                wx.redirectTo({
                  url: '../orders/orders',
                })
              })
              wx.call("payment", {
                command: "payOK",
                out_trade_no: f.order_number
              });
            } else {
              wx.toast('网络错误，提交失败')
            }
          })
        },
        fail: () => {
          this.setData({ disabled: false })
          wx.toast('已取消提交订单')
        },
      })
    }, fail => {
      this.setData({ disabled: false })
      console.log(fail);
    })
  },

  calcPrice() {
    console.log(var_a, this.data.formatIndex, 'b', var_b,'c', var_c, (this.data.floorIndex + 1));
    this.setData({ 
      price: parseFloat(var_a * this.data.formatIndex + var_b + var_c * (this.data.floorIndex + 1)).toFixed(2) 
    })
  },

  formatPicker(e) {
    this.setData({ formatIndex: parseInt(e.detail.value) })
    this.calcPrice();
  },

  floorPicker(e) {
    this.setData({ floorIndex: parseInt(e.detail.value) })
    this.calcPrice();
  },

  floorChange(e) {
    this.setData({ showFloorSwitch: e.detail.value });
    if (this.data.showFloorSwitch) 
      this.setData({ floorIndex: 0 })
    else 
      this.setData({ floorIndex: -1 })
    this.calcPrice();
  },

  imageChange(e) {
    this.setData({ showImageSwitch: e.detail.value });
  },

  noteInput(e) {
    this.setData({ note: e.detail.value });
  },

  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        console.log(res);
        
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '哦豁',
      content: '确定要把照片删掉嘛？',
      cancelText: '还能用',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
})